Array.prototype.unique = function(key) {
  var arr = this;
  //var n = [arr[0]];
  var n = [];
  for (var i = 1; i < arr.length; i++) {
    if (key === undefined) {
      if (n.indexOf(arr[i]) == -1) n.push(arr[i]);
    } else {
      inner: {
        var has = false;
        for (var j = 0; j < n.length; j++) {
          if (arr[i][key] == n[j][key]) {
            has = true;
            break inner;
          }
        }
      }
      if (!has) {
        n.push(arr[i]);
      }
    }
  }
  return n;
};

//数组中的 json去重
Array.prototype.jsonRepeat = function(key,type){
  for(var j=0;j<this.length;j++){
    if(j <this.length){
      var one = this[j][key];
      for(var i=j+1 ; i<this.length ;i++){
        if(one == this[i][key]){
          if(type == 'pre'){
            this.splice(i, 1);//删除的是后者   直接改变原数组
            i--;
          }else{
            this.splice(j, 1);//删除的是前者,默认删除前者
            j--;
            break
          }
        }
      }
    }
  }
  return this;
};


//数组去重  不改变原数组,返回一个新数组.挺好
window.arrRepeat = (arr) => {

  let result = new Set(arr);

  let newArr = Array.from(result);

  return newArr

};

//年月日剪切 以及星期转换
String.prototype.dayCut = function(){
  return this.substring(8,10);
};

String.prototype.monthCut = function(){
  return this.substring(5,7);
};

String.prototype.yearCut = function(){
  return this.substring(0,4);
};

String.prototype.weekGet = function(){
  var x = new Array("周日", "周一", "周二","周三","周四", "周五","周六");
  return x[new Date(this).getDay()]
};

Number.prototype.weekGet = function(){
  var x = new Array("周日", "周一", "周二","周三","周四", "周五","周六");
  return x[new Date(this).getDay()]
};

Number.prototype.num9 = function(){
  var num = parseInt(this);
  if (num < 10) {
    num = '0' + num;
    return num;
  } else {
    return num;
  }
};

String.prototype.num9 = function(){
  var num = parseInt(this);
  if (num < 10) {
    num = '0' + num;
    return num;
  } else {
    return num;
  }
};


//以今天为起点 拿到未来的k天   今天为0 明天为1   例如想拿到第20天: '20'.future()  拿到今天为 '0'.future()
String.prototype.future = function(){

  var day3 = new Date();
  day3.setTime(day3.getTime() + 24 * 60 * 60 * 1000 * this);

  var s3 = day3.getFullYear() + "-" + (day3.getMonth() + 1).num9() + "-" + day3.getDate().num9();
  return s3

};

//以任意一天为起点 拿到之前或者未来的第N天

String.prototype.futureOrPast = function(){

  var cuo = Date.parse(this);
  if(!arguments[0].future && !arguments[0].past ){
    cuo = cuo + 24 * 60 * 60 * 1000 * arguments[0];
  }

  if(arguments[0].past){alert(11); cuo = cuo - 24 * 60 * 60 * 1000 * arguments[0].past};
  if(arguments[0].future){ alert(111); cuo = cuo - 24 * 60 * 60 * 1000 * arguments[0].future};

  return cuo.cuoChangeTime();

};

//拿到当前时间 用上面的方法就好了.拿当前 年 月  日  及今天星期几
function getCurrentTimeFun (sel){
  var currentTimeCuo = Date.now();
  var time = new Date();
  var currentYear = time.getFullYear();
  var currentMonth = (time.getMonth() + 1).toString().num9();
  var currentDay = (time.getDate()).toString().num9();
  var currentTime = currentYear + '-' + currentMonth + '-' + currentDay;
  var weeks = new Array("周日", "周一", "周二","周三","周四", "周五","周六");
  var week =  weeks[new Date(currentTime).getDay()];
  switch (sel){
    case undefined:
      return currentTime;
    case 'year':
      return currentYear;
    case 'month':
      return currentMonth;
    case 'day':
      return currentDay;
    case 'week':
      return week;
    case 'cuo':
      return currentTimeCuo;
  }
}

String.prototype.getCurrentTime = function(){
  var sel = this;
  var currentTimeCuo = Date.now();
  var time = new Date();
  var currentYear = time.getFullYear();
  var currentMonth = (time.getMonth() + 1).toString().num9();
  var currentDay = (time.getDate()).toString().num9();
  var currentTime = currentYear + '-' + currentMonth + '-' + currentDay;
  var weeks = new Array("周日", "周一", "周二","周三","周四", "周五","周六");
  var week =  weeks[new Date(currentTime).getDay()];
  switch (sel){
    case undefined:
      return currentTime;
    case 'time':
      return currentTime;
    case 'year':
      return currentYear;
    case 'month':
      return currentMonth;
    case 'day':
      return currentDay;
    case 'week':
      return week;
    case 'cuo':
      return currentTimeCuo;
    case 'year-month':
      return currentYear + '-' + currentMonth;
  }
};

function cuoChangeTime(that,sel){
  var time = new Date();
  time.setTime(that);
  var detail = time.toLocaleString();//2017-4-6 15:55:06
  var newDate = new Date(that);
  var year = newDate.getFullYear();
  var month = newDate.getMonth() + 1;
  var day = newDate.getDate();
  var hours = newDate.getHours();
  var minutes = newDate.getMinutes();
  var seconds = newDate.getSeconds();
  switch (sel){
    case 'time':
      return year + '-' + month.num9() + '-' + day.num9() ;
    case 'year':
      return year;
    case 'month':
      return month.num9();
    case 'day':
      return day.num9();
    case 'hours':
      return hours.num9();
    case 'minutes':
      return minutes.num9();
    case 'seconds':
      return seconds.num9();
    case 'detail':
      return detail;
    case 'timeC':
      return year + '年' + month.num9() + '月' + day.num9() + '日';
    default:
      return year + '-' + month.num9() + '-' + day.num9() ;
  }
}

//时间戳转年月日,时分秒..后台给的时间戳一般都是Number
Number.prototype.cuoChangeTime = function(sel){
  var that = this;
  return cuoChangeTime(that,sel)
};

String.prototype.cuoChangeTime = function(sel){
  if(!this){
    return null;
  }
  return new Date(this.replace(/-/g, "/")).getTime().cuoChangeTime(sel);
};



//前端input框模糊筛选
Array.prototype.search = function(keyWord){

  var list = JSON.parse(JSON.stringify(this));
  //正则表达式
  var len = list.length;
  var arr = [];
  var reg = new RegExp(keyWord);
  for (var i = 0; i < len; i++) {
    if (list[i].name.match(reg)) {
      arr.push(list[i]);
    }
  }
  return arr;


  //字符串方法indexOf
  /*  var len = list.length;
   var arr = [];
   for (var i = 0; i < len; i++) {
   //如果字符串中不包含目标字符会返回-1
   if (list[i].name.indexOf(keyWord) >= 0) {
   arr.push(list[i]);
   }
   }
   return arr;*/

};

//数组神拷贝
Array.prototype.copyArr = function(){
  var result = this.map((item)=>item);
  return result
};


//JSON神拷贝
/*
  var obj = {
    kk:'1',
    jj:'2'
  };
  var result = JSON.parse(JSON.stringify(obj));
*/



window.app=app||{};
app.ajax=function(options){
  //这是二次封装的ajax方法，对请求的服务器的数据做预处理
  // $.ajax(options)
  //console.log("进入封装，可以对url，method,options进行操作")
  var d =$.Deferred();
  $.ajax(options).done(function(data) {
    /*var d = handle(data);       // 这里按你的需求处理 data*/
    // d.resolve(d);
    //console.log("done 中的data")
    //console.log(typeof data)
    //console.log(data.length)
    //console.log(data)
    if (data.code==99){
      //console.log("code 99")
      //window.location.href= '/index.html';
    }
    d.resolve(data);
  }).fail(function() {
    d.reject();
  });
  return d;
};


/*//模块内调用ajax
app.ajax({
  url:"",
  method:'post'
}).done(function(resp){
  console.info("我希望在这里获取到的resp是我在公用方法预处理过的数据",resp)
});*/

export{getCurrentTimeFun}
